﻿using System;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace CommandRunner.Model
{
    /// <summary>
    /// This class describes a single command to be run
    /// </summary>
    [DataContract]
    [Serializable]
    [XmlType(TypeName = "Command")]
    public class SimpleCommand
    {
        /// <summary>
        /// Should the system wait for this command to finish before running the next command in the series
        /// </summary>
        [DataMember]
        [XmlAttribute("Wait")]
        public bool Wait;

        /// <summary>
        /// Should the entire command run be considered a failure if this command fails (this is pre-emptive)
        /// </summary>
        [DataMember]
        [XmlAttribute("MonitorSuccess")]
        public bool Monitor;

        /// <summary>
        /// If a value is specified the command will be passed off to a Command Runner server running on the spedified server
        /// </summary>
        [DataMember(IsRequired = false)]
        [XmlAttribute("Server")]
        public string Server = null;

        /// <summary>
        /// If a Server is specified this is the port that the command will be sent to
        /// </summary>
        [DataMember(IsRequired = false)]
        [XmlAttribute("Port")]
        public int Port = 48888;


        /// <summary>
        /// If InteractiveWindow is true then the process will be spawned in such a way that the ui will be visible and interactive.  This is useful for UI automation.
        /// </summary>
        [DataMember(IsRequired = false)]
        [XmlAttribute("InteractiveWindow")]
        public bool InteractiveWindow = false;

        /// <summary>
        /// The working directory for the command
        /// </summary>
        [DataMember(IsRequired = false)]
        [XmlAttribute("WorkingDirectory")]
        public string WorkingDirectory = null;

        /// <summary>
        /// A string containing the entire command to be executed using cmd /c
        /// </summary>
        [DataMember]
        [XmlText]
        public string Execute;

        public override string ToString()
        {
            return string.Format("command {0}{1}{2}{3}", Execute, WorkingDirectory != null ? " from working directory " + WorkingDirectory : "", Monitor ? " monitoring success" : "", Wait ? " waiting for execution to complete." : ".");
        }
    }
}
